From a478511e8fd215ad878627d2516cb9253a2ab12f Mon Sep 17 00:00:00 2001
From: Toni Kukurin <tkukurin@gmail.com>
Date: Sun, 23 Feb 2020 09:52:36 +0100
Subject: [PATCH 1/3] Update ui.py

Fix 'FileNotFound' if `screen` is not available
---
 ranger/gui/ui.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/ranger/gui/ui.py b/ranger/gui/ui.py
index d2dbb759d..5ffdf4a47 100644
--- a/ranger/gui/ui.py
+++ b/ranger/gui/ui.py
@@ -497,7 +497,7 @@ def handle_multiplexer(self):
                 # gives out a warning if $TERM is not "screen"
                 try:
                     self._screen_title = check_output(
-                        ['screen', '-Q', 'title']).strip()
+                        ['screen', '-Q', 'title'], shell=True).strip()
                 except CalledProcessError:
                     self._screen_title = None
 

From 1cdcce0a44d031be13009a70e25cc4cf75789eac Mon Sep 17 00:00:00 2001
From: toonn <toonn@toonn.io>
Date: Mon, 2 Mar 2020 20:55:17 +0100
Subject: [PATCH 2/3] Change approach to multiplexer title renaming

There were a couple bugs causing ranger to miss opportunities to restore
multiplexer window names in tmux.

Fixes #1805
---
 ranger/gui/ui.py | 99 ++++++++++++++++++++++++++++--------------------
 1 file changed, 58 insertions(+), 41 deletions(-)

diff --git a/ranger/gui/ui.py b/ranger/gui/ui.py
index 5ffdf4a47..dc4379acc 100644
--- a/ranger/gui/ui.py
+++ b/ranger/gui/ui.py
@@ -9,6 +9,7 @@
 import curses
 from subprocess import CalledProcessError
 
+from ranger.ext.get_executables import get_executables
 from ranger.ext.keybinding_parser import KeyBuffer, KeyMaps, ALT_KEY
 from ranger.ext.lazy_property import lazy_property
 from ranger.ext.signals import Signal
@@ -49,6 +50,16 @@ def _setup_mouse(signal):
         curses.mousemask(0)
 
 
+def _in_tmux():
+    return ('TMUX' in os.environ
+            and 'tmux' in get_executables())
+
+
+def _in_screen():
+    return ('screen' in os.environ['TERM']
+            and 'screen' in get_executables())
+
+
 class UI(  # pylint: disable=too-many-instance-attributes,too-many-public-methods
         DisplayableContainer):
     ALLOWED_VIEWMODES = 'miller', 'multipane'
@@ -73,8 +84,8 @@ def __init__(self, env=None, fm=None):  # pylint: disable=super-init-not-called
         self.multiplexer = None
         self._draw_title = None
         self._tmux_automatic_rename = None
-        self._tmux_title = None
-        self._screen_title = None
+        self._multiplexer_title = None
+        self._multiplexer_title = None
         self.browser = None
 
         if fm is not None:
@@ -469,58 +480,64 @@ def throbber(self, string='.', remove=False):
     # Handles window renaming behaviour of the terminal multiplexers
     # GNU Screen and Tmux
     def handle_multiplexer(self):
-        if self.settings.update_tmux_title:
-            if 'TMUX' in os.environ:
-                # Stores the automatic-rename setting
-                # prints out a warning if the allow-rename in tmux is not set
-                tmux_allow_rename = check_output(
-                    ['tmux', 'show-window-options', '-v',
-                     'allow-rename']).strip()
-                if tmux_allow_rename == 'off':
-                    self.fm.notify('Warning: allow-rename not set in Tmux!',
-                                   bad=True)
-                elif self._tmux_title is None:
-                    self._tmux_title = check_output(
-                        ['tmux', 'display-message', '-p', '#W']).strip()
-                else:
+        if (self.settings.update_tmux_title and not self._multiplexer_title):
+            try:
+                if _in_tmux():
+                    # Stores the automatic-rename setting
+                    # prints out a warning if allow-rename isn't set in tmux
                     try:
+                        tmux_allow_rename = check_output(
+                            ['tmux', 'show-window-options', '-v',
+                             'allow-rename']).strip()
+                    except CalledProcessError:
+                        tmux_allow_rename = 'off'
+                    if tmux_allow_rename == 'off':
+                        self.fm.notify('Warning: allow-rename not set in Tmux!',
+                                       bad=True)
+                    else:
+                        self._multiplexer_title = check_output(
+                            ['tmux', 'display-message', '-p', '#W']).strip()
                         self._tmux_automatic_rename = check_output(
                             ['tmux', 'show-window-options', '-v',
                              'automatic-rename']).strip()
                         if self._tmux_automatic_rename == 'on':
                             check_output(['tmux', 'set-window-option',
                                           'automatic-rename', 'off'])
-                    except CalledProcessError:
-                        pass
-            elif 'screen' in os.environ['TERM'] and self._screen_title is None:
-                # Stores the screen window name before renaming it
-                # gives out a warning if $TERM is not "screen"
-                try:
-                    self._screen_title = check_output(
-                        ['screen', '-Q', 'title'], shell=True).strip()
-                except CalledProcessError:
-                    self._screen_title = None
+                elif _in_screen():
+                    # Stores the screen window name before renaming it
+                    # gives out a warning if $TERM is not "screen"
+                    self._multiplexer_title = check_output(
+                        ['screen', '-Q', 'title']).strip()
+            except CalledProcessError:
+                self.fm.notify("Couldn't access previous multiplexer window"
+                               " name, won't be able to restore.",
+                               bad=False)
+            if not self._multiplexer_title:
+                self._multiplexer_title = os.environ.get(
+                    "SHELL",
+                    "shell").split("/")[-1]
 
             sys.stdout.write("\033kranger\033\\")
             sys.stdout.flush()
 
     # Restore window name
     def restore_multiplexer_name(self):
-        try:
-            if 'TMUX' in os.environ:
-                if self._tmux_automatic_rename:
-                    check_output(['tmux', 'set-window-option',
-                                  'automatic-rename',
-                                  self._tmux_automatic_rename])
-                else:
-                    check_output(['tmux', 'set-window-option', '-u',
-                                  'automatic-rename'])
-                if self._tmux_title:
-                    check_output(['tmux', 'rename-window', self._tmux_title])
-            elif 'screen' in os.environ['TERM'] and self._screen_title:
-                check_output(['screen', '-X', 'title', self._screen_title])
-        except CalledProcessError:
-            self.fm.notify("Could not restore window-name!", bad=True)
+        if self._multiplexer_title:
+            try:
+                if _in_tmux():
+                    if self._tmux_automatic_rename:
+                        check_output(['tmux', 'set-window-option',
+                                      'automatic-rename',
+                                      self._tmux_automatic_rename])
+                    else:
+                        check_output(['tmux', 'set-window-option', '-u',
+                                      'automatic-rename'])
+            except CalledProcessError:
+                self.fm.notify("Could not restore multiplexer window name!",
+                               bad=True)
+
+            sys.stdout.write("\033k{}\033\\".format(self._multiplexer_title))
+            sys.stdout.flush()
 
     def hint(self, text=None):
         self.status.hint = text

From 2460c2ad6a2f528104deb263a81086e03713081e Mon Sep 17 00:00:00 2001
From: toonn <toonn@toonn.io>
Date: Wed, 4 Mar 2020 11:47:21 +0100
Subject: [PATCH 3/3] Drop double setting and prefer basename over split

---
 ranger/gui/ui.py | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/ranger/gui/ui.py b/ranger/gui/ui.py
index dc4379acc..a2ea77784 100644
--- a/ranger/gui/ui.py
+++ b/ranger/gui/ui.py
@@ -85,7 +85,6 @@ def __init__(self, env=None, fm=None):  # pylint: disable=super-init-not-called
         self._draw_title = None
         self._tmux_automatic_rename = None
         self._multiplexer_title = None
-        self._multiplexer_title = None
         self.browser = None
 
         if fm is not None:
@@ -513,9 +512,8 @@ def handle_multiplexer(self):
                                " name, won't be able to restore.",
                                bad=False)
             if not self._multiplexer_title:
-                self._multiplexer_title = os.environ.get(
-                    "SHELL",
-                    "shell").split("/")[-1]
+                self._multiplexer_title = os.path.basename(
+                    os.environ.get("SHELL", "shell"))
 
             sys.stdout.write("\033kranger\033\\")
             sys.stdout.flush()
